package com.example.apidemo.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author dzx
 * @date 2024/6/17
 */

@Service
public class JedisUtil {
    @Autowired
    static RedisTemplate redisTemplate;

    public JedisUtil(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 获取缓存
     *
     * @param key id
     * @return string
     */
    public static Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置缓存
     *
     * @param dbId    库号
     * @param key     id
     * @param value   值
     * @param seconds 时间
     */
    public static void set(Integer dbId, String key, String value, Integer seconds) {

        if (key == null || value == null) {
            throw new IllegalArgumentException("key和value不能为空");
        }
        try {
            if (seconds == null || seconds == 0) {
                redisTemplate.opsForValue().set(key, value);
            } else {
                redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            // 适当的错误处理机制
            System.err.println("操作失败: " + e.getMessage());
        }

    }

}
/*{
    private final int dbIndex = 0;
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    public Object get(String key) {
        jedis.select(dbIndex);
        String value = jedis.get(key);
        //jedis.close();
        return value;
    }

    public Object get(int dbId, String key) {
        if (dbId > 0) {
            jedis.select(dbId);
        } else {
            jedis.select(dbIndex);
        }
        String value = jedis.get(key);
        //jedis.close();
        return value;
    }

    public void set(String key, String value) {
        jedis.select(dbIndex);
        jedis.set(key, value);
        //jedis.close();
    }

    public void set(String key, String value, int seconds) {
        jedis.select(dbIndex);
        jedis.set(key, value);
        if (seconds > 0) {
            jedis.expire(key, seconds);
        }
        //jedis.close();
    }

    public void set(int dbId, String key, String value) {
        if (dbId > 0) {
            jedis.select(dbId);
        } else {
            jedis.select(dbIndex);
        }
        jedis.set(key, value);

        //jedis.close();
    }

    public void set(Integer dbId, String key, String value, Integer seconds) {
        if (key == null || value == null) {
            throw new IllegalArgumentException("key和value不能为空");
        }
        try {
            // 根据 dbId 选择数据库
            jedis.select(dbId != null && dbId > 0 ? dbId : dbIndex);

            // 设置键值对
            jedis.set(key, value);

            // 设置过期时间
            if (seconds != null && seconds > 0) {
                jedis.expire(key, seconds);
            }
        } catch (Exception e) {
            // 适当的错误处理机制
            System.err.println("操作失败: " + e.getMessage());
        }
    }

    public void expire(Integer dbId, String key, Integer seconds) {
        if (dbId != null) {
            jedis.select(dbId);
        } else {
            jedis.select(dbIndex);
        }
        if (seconds > 0) {
            jedis.expire(key, seconds);
        }
        //jedis.close();
    }
}
*/
